
clear
do "...\First.do"
set scheme s2mono

********************************************************************************
use "$work\analysis_sample.dta"
keep if inrange(age,40,70)

g help=1 if inrange(timing,-4,0)
bys pnr: egen N_pre_2=total(help)
tab N_pre_2
keep if N_pre_2==5

** Lung scans only for women
** Statins only for men
replace first_lung=. if male==1
replace statins=. if male==0

* Translate to US dollars
replace total_cost_GP=total_cost_GP/7.5


********************************************************************************
* Figure 2
********************************************************************************

foreach var of varlist death  {


cap drop beta ci_u ci_l
g beta=.
g ci_u=.
g ci_l=.

cap drop beta2 ci_u2 ci_l2
g beta2=.
g ci_u2=.
g ci_l2=.

areg `var' ib5.t##help3 $patient_X $gp_X if low_ses==1 & inrange(t,5,10), cluster(new_gp) a(gp_fe)


forvalues i=5/10 {
*if `i'!=4{
replace beta=_b[`i'.t#1.help3] if t==`i'
replace ci_u=_b[`i'.t#1.help3]+invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
replace ci_l=_b[`i'.t#1.help3]-invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
*}
}

replace beta=0 if t==5
replace ci_u=0 if t==5
replace ci_l=0 if t==5

areg `var' ib5.t##help3 $patient_X $gp_X if low_ses==0 & inrange(t,5,10), cluster(new_gp) a(gp_fe)


forvalues i=5/10 {
*if `i'!=5{
replace beta2=_b[`i'.t#1.help3] if t==`i'
replace ci_u2=_b[`i'.t#1.help3]+invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
replace ci_l2=_b[`i'.t#1.help3]-invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
*}
}



replace beta2=0 if t==5
replace ci_u2=0 if t==5
replace ci_l2=0 if t==5



preserve
keep timing beta ci_u ci_l beta2 ci_u2 ci_l2
duplicates drop

keep if inrange(timing,0,5)

foreach v of varlist beta ci_u ci_l beta2 ci_u2 ci_l2 {
	replace `v'=`v'*100
}

sort timing
tw (rcap ci_l2 ci_u2 timing, color(black%50) lc(*0.5)) (connect beta2 timing, mcolor(black%50) lcolor(navy%75) lp(dash) msymbol(diamond)) (rcap ci_l ci_u timing, color(black%50) lc(*0.5)) (connect beta timing, lcolor(black) mcolor(black) graphregion(color(white) ilcolor(white) lcolor(white)) bgcolor(white) legend(order(2 "High SES patients" 4 "Low SES patients") region(lwidth(none))) xtitle("Years since clinic closure") ytitle("Percentage Points") xscale(range(-.5 5.5)) xlabel(0(1)5) xline(0, lp(dash) lc(*0.5)) yline(0, lc(*0.5))) 
graph export "$fig\Fig2_`var'_bw.png", replace 
graph export "$fig\Fig2_`var'_bw.pdf", replace 
 
restore

}


********************************************************************************
* Figure 3
********************************************************************************




foreach var of varlist N_visits2  {


cap drop beta ci_u ci_l
g beta=.
g ci_u=.
g ci_l=.

cap drop beta2 ci_u2 ci_l2
g beta2=.
g ci_u2=.
g ci_l2=.

areg `var' ib4.t##help3 $patient_X if low_ses==1, a(id) cluster(id)


forvalues i=1/10 {
if `i'!=4{
replace beta=_b[`i'.t#1.help3] if t==`i'
replace ci_u=_b[`i'.t#1.help3]+invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
replace ci_l=_b[`i'.t#1.help3]-invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
}
}

replace beta=0 if t==4
replace ci_u=0 if t==4
replace ci_l=0 if t==4

areg `var' ib4.t##help3 $patient_X if low_ses==0, a(id) cluster(id)


forvalues i=1/10 {
if `i'!=4{
replace beta2=_b[`i'.t#1.help3] if t==`i'
replace ci_u2=_b[`i'.t#1.help3]+invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
replace ci_l2=_b[`i'.t#1.help3]-invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
}
}



replace beta2=0 if t==4
replace ci_u2=0 if t==4
replace ci_l2=0 if t==4



preserve
keep timing beta ci_u ci_l beta2 ci_u2 ci_l2
duplicates drop

foreach v of varlist beta ci_u ci_l beta2 ci_u2 ci_l2 {
	replace `v'=`v'
}

sort timing
tw (rcap ci_l2 ci_u2 timing, color(black%50) lc(*0.5)) (connect beta2 timing, mcolor(black%50) lcolor(black%50) lp(dash) msymbol(diamond)) (rcap ci_l ci_u timing, color(black%50) lc(*0.5)) (connect beta timing, lcolor(black) mcolor(black) graphregion(color(white) ilcolor(white) lcolor(white)) bgcolor(white) legend(order(2 "High SES patients" 4 "Low SES patients") region(lwidth(none))) xtitle("Years since clinic closure") ytitle("Number of visits") xscale(range(-4 5)) xlabel(-4(1)5) xline(0, lp(dash) lc(*0.5)) yline(0, lc(*0.5))) 
graph export "$fig\Fig3_`var'_bw.png", replace 
graph export "$fig\Fig3_`var'_bw.pdf", replace 
 
restore

}


foreach var of varlist total_cost_GP  {


cap drop beta ci_u ci_l
g beta=.
g ci_u=.
g ci_l=.

cap drop beta2 ci_u2 ci_l2
g beta2=.
g ci_u2=.
g ci_l2=.

areg `var' ib4.t##help3 $patient_X if low_ses==1, a(id) cluster(id)


forvalues i=1/10 {
if `i'!=4{
replace beta=_b[`i'.t#1.help3] if t==`i'
replace ci_u=_b[`i'.t#1.help3]+invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
replace ci_l=_b[`i'.t#1.help3]-invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
}
}

replace beta=0 if t==4
replace ci_u=0 if t==4
replace ci_l=0 if t==4

areg `var' ib4.t##help3 $patient_X if low_ses==0, a(id) cluster(id)


forvalues i=1/10 {
if `i'!=4{
replace beta2=_b[`i'.t#1.help3] if t==`i'
replace ci_u2=_b[`i'.t#1.help3]+invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
replace ci_l2=_b[`i'.t#1.help3]-invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
}
}



replace beta2=0 if t==4
replace ci_u2=0 if t==4
replace ci_l2=0 if t==4



preserve
keep timing beta ci_u ci_l beta2 ci_u2 ci_l2
duplicates drop

sort timing
tw (rcap ci_l2 ci_u2 timing, color(black%50) lc(*0.5)) (connect beta2 timing, mcolor(black%50) lcolor(black%50) lp(dash) msymbol(diamond)) (rcap ci_l ci_u timing, color(black%50) lc(*0.5)) (connect beta timing, lcolor(black) mcolor(black) graphregion(color(white) ilcolor(white) lcolor(white)) bgcolor(white) legend(order(2 "High SES patients" 4 "Low SES patients") region(lwidth(none))) xtitle("Years since clinic closure") ytitle("US Dollars") xscale(range(-4 5)) xlabel(-4(1)5) xline(0, lp(dash) lc(*0.5)) yline(0, lc(*0.5))) 
graph export "$fig\Fig3_`var'_bw.png", replace 
graph export "$fig\Fig3_`var'_bw.pdf", replace 
 

restore

}




foreach var of varlist  statins diabetes_control ACSC_COPD first_lung {


cap drop beta ci_u ci_l
g beta=.
g ci_u=.
g ci_l=.

cap drop beta2 ci_u2 ci_l2
g beta2=.
g ci_u2=.
g ci_l2=.

areg `var' ib4.t##help3 $patient_X if low_ses==1, a(id) cluster(id)


forvalues i=1/10 {
if `i'!=4{
replace beta=_b[`i'.t#1.help3] if t==`i'
replace ci_u=_b[`i'.t#1.help3]+invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
replace ci_l=_b[`i'.t#1.help3]-invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
}
}

replace beta=0 if t==4
replace ci_u=0 if t==4
replace ci_l=0 if t==4

areg `var' ib4.t##help3 $patient_X if low_ses==0, a(id) cluster(id)


forvalues i=1/10 {
if `i'!=4{
replace beta2=_b[`i'.t#1.help3] if t==`i'
replace ci_u2=_b[`i'.t#1.help3]+invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
replace ci_l2=_b[`i'.t#1.help3]-invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
}
}



replace beta2=0 if t==4
replace ci_u2=0 if t==4
replace ci_l2=0 if t==4



preserve
keep timing beta ci_u ci_l beta2 ci_u2 ci_l2
duplicates drop

foreach v of varlist beta ci_u ci_l beta2 ci_u2 ci_l2 {
	replace `v'=`v'*100
}

sort timing
tw (rcap ci_l2 ci_u2 timing, color(black%50) lc(*0.5)) (connect beta2 timing, mcolor(black%50) lcolor(black%50) lp(dash) msymbol(diamond)) (rcap ci_l ci_u timing, color(black%50) lc(*0.5)) (connect beta timing, lcolor(black) mcolor(black) graphregion(color(white) ilcolor(white) lcolor(white)) bgcolor(white) legend(order(2 "High SES patients" 4 "Low SES patients") region(lwidth(none))) xtitle("Years since clinic closure") ytitle("Percentage Points") xscale(range(-4 5)) xlabel(-4(1)5) xline(0, lp(dash) lc(*0.5)) yline(0, lc(*0.5))) 
graph export "$fig\Fig3_`var'_bw.png", replace
graph export "$fig\Fig3_`var'_bw.pdf", replace 
 

restore

}


********************************************************************************
* FIGUR A1 -Balanced
********************************************************************************

bys pnr: g N_pnr=_N
tab N_pnr
g balance=(N_pnr==10)

g stay_5_years=(new_gp==ydernr & timing==5)
tab stay_5_years if timing==5
bys pnr: egen max_5years=max(stay_5_years)


*Panel A
foreach var of varlist N_visits2  {


cap drop beta ci_u ci_l
g beta=.
g ci_u=.
g ci_l=.

cap drop beta2 ci_u2 ci_l2
g beta2=.
g ci_u2=.
g ci_l2=.

areg `var' ib4.t##help3 $patient_X if low_ses==1 & balance==1, a(id) cluster(id)


forvalues i=1/10 {
if `i'!=4{
replace beta=_b[`i'.t#1.help3] if t==`i'
replace ci_u=_b[`i'.t#1.help3]+invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
replace ci_l=_b[`i'.t#1.help3]-invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
}
}

replace beta=0 if t==4
replace ci_u=0 if t==4
replace ci_l=0 if t==4

areg `var' ib4.t##help3 $patient_X if low_ses==0 & balance==1, a(id) cluster(id)


forvalues i=1/10 {
if `i'!=4{
replace beta2=_b[`i'.t#1.help3] if t==`i'
replace ci_u2=_b[`i'.t#1.help3]+invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
replace ci_l2=_b[`i'.t#1.help3]-invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
}
}



replace beta2=0 if t==4
replace ci_u2=0 if t==4
replace ci_l2=0 if t==4



preserve
keep timing beta ci_u ci_l beta2 ci_u2 ci_l2
duplicates drop

foreach v of varlist beta ci_u ci_l beta2 ci_u2 ci_l2 {
	replace `v'=`v'
}

sort timing
tw (rcap ci_l2 ci_u2 timing, color(navy%50) lc(*0.5)) (connect beta2 timing, mcolor(navy%75) lcolor(navy%75) lp(dash) msymbol(diamond)) (rcap ci_l ci_u timing, color(sienna%50) lc(*0.5)) (connect beta timing, lcolor(sienna%75) mcolor(sienna%75) graphregion(color(white) ilcolor(white) lcolor(white)) bgcolor(white) legend(order(2 "High SES patients" 4 "Low SES patients") region(lwidth(none))) xtitle("Years since clinic closure") ytitle("Number of visits") xscale(range(-4 5)) xlabel(-4(1)5) xline(0, lp(dash) lc(*0.5)) yline(0, lc(*0.5))) 
graph export "$fig\FigA1_PanelA.png", replace 

restore

}




* Panel B
foreach var of varlist N_visits2  {


cap drop beta ci_u ci_l
g beta=.
g ci_u=.
g ci_l=.

cap drop beta2 ci_u2 ci_l2
g beta2=.
g ci_u2=.
g ci_l2=.

areg `var' ib4.t##help3 $patient_X if low_ses==1 & max_5years==1, a(id) cluster(id)


forvalues i=1/10 {
if `i'!=4{
replace beta=_b[`i'.t#1.help3] if t==`i'
replace ci_u=_b[`i'.t#1.help3]+invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
replace ci_l=_b[`i'.t#1.help3]-invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
}
}

replace beta=0 if t==4
replace ci_u=0 if t==4
replace ci_l=0 if t==4

areg `var' ib4.t##help3 $patient_X if low_ses==0 & max_5years==1, a(id) cluster(id)


forvalues i=1/10 {
if `i'!=4{
replace beta2=_b[`i'.t#1.help3] if t==`i'
replace ci_u2=_b[`i'.t#1.help3]+invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
replace ci_l2=_b[`i'.t#1.help3]-invttail(e(df_r),0.025)*_se[`i'.t#1.help3] if t==`i'
}
}



replace beta2=0 if t==4
replace ci_u2=0 if t==4
replace ci_l2=0 if t==4



preserve
keep timing beta ci_u ci_l beta2 ci_u2 ci_l2
duplicates drop

foreach v of varlist beta ci_u ci_l beta2 ci_u2 ci_l2 {
	replace `v'=`v'
}

sort timing
tw (rcap ci_l2 ci_u2 timing, color(navy%50) lc(*0.5)) (connect beta2 timing, mcolor(navy%75) lcolor(navy%75) lp(dash) msymbol(diamond)) (rcap ci_l ci_u timing, color(sienna%50) lc(*0.5)) (connect beta timing, lcolor(sienna%75) mcolor(sienna%75) graphregion(color(white) ilcolor(white) lcolor(white)) bgcolor(white) legend(order(2 "High SES patients" 4 "Low SES patients") region(lwidth(none))) xtitle("Years since clinic closure") ytitle("Number of visits") xscale(range(-4 5)) xlabel(-4(1)5) xline(0, lp(dash) lc(*0.5)) yline(0, lc(*0.5))) 
graph export "$fig\FigA1_PanelB.png", replace 

restore

}






